import modelExtend from 'dva-model-extend';
import { query, queryDistributorChilds, queryDistributorDetail, editDistributor, queryDistributorLevels } from './services/distributorManageService';
import { lwjPageModel } from 'utils/model';
import { routerRedux } from 'dva/router';
import { registryTypes } from "utils/enumConfig";

let _tempItem = {};
export default modelExtend(lwjPageModel, {

  namespace: "distributorList",

  state: {
    isExpand: false,
    queryParam: {},
    pagination: {
      current: 1,
      total: 0,
      showTotal: total => `共 ${total} 条`,
      showSizeChanger: true,
      showQuickJumper: true,
      defaultPageSize: 10,
      pageSize: 10
    },
    list: [],
    detailVisible: false,
    editVisible: false,
    currentItem: {},
    currentIndex: null,
    levels: [],
    registryTypes: registryTypes,
    registryTypeList: [],
    hash: 0
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === "/wx/distributorManage/distributorList") {
          dispatch({
            type: 'reset'
          })
          dispatch({ 
            type: 'query', 
            payload: {
              limit: 10,
              start: 0
            } 
          })
        }
      })
    }

  },

  effects: {
    *query({payload}, { select, call, put }) {
      const queryParam = yield select(({ distributorList }) => distributorList.queryParam);
      const { result: data } = yield call(query, {
        start: payload.start,
        limit: payload.limit,
        sort: 'createTime',
        order: 'desc',
        isRootQuery: (JSON.stringify(queryParam)=="{}" ? true : false),
        ...queryParam
      });
      const hash = yield select(({ distributorList }) => distributorList.hash);
      data.content.map((val) => {
        val.key = val.id;
        val.id = hash + '-' + val.id;
        if(val.childrenNode > 0){
          val.children = [];
        }
        return val;
      })
      yield put({
        type: 'setDistributorList',
        payload: {
          list: data.content,
          pagination: {
            current: payload.start + 1,
            total: data.totalElements,
            showTotal: total => `共 ${total} 条`,
            showSizeChanger: true,
            showQuickJumper: true,
            defaultPageSize: 10,
            pageSize: data.size
          },
          hash: hash + 1
        },
      });
    },
    *queryDistributorChilds({currentItem}, { select, call, put}) {
      const {result: data} = yield call(queryDistributorChilds, {
        invitationCode: currentItem.invitationCode
      });
      data.map((val) => {
        val.key = val.id;
        val.id = currentItem.id + '-' + val.id;
        if(val.childrenNode > 0){
          val.children = [];
        }
        return val;
      })
      currentItem.children = data;
      const list = yield select(({ distributorList }) => distributorList.list);
      const pagination = yield select(({ distributorList }) => distributorList.pagination);
      const hash = yield select(({ distributorList }) => distributorList.hash);
      yield put({
        type: 'setDistributorList',
        payload: {
          list: list,
          pagination: pagination,
          hash: hash
        },
      });
    },
    *queryDistributorDetail({currentItem,field,flag}, { select, call, put}) {
      const {result: data} = yield call(queryDistributorDetail, {
        id: currentItem.key
      });
      _tempItem = currentItem;
      data.enterPicArr = (data.enterPic ? data.enterPic.split(';') : null);
      yield put({
        type: 'toggleModal',
        currentItem: data,
        field: field,
        flag: flag
      });
    },
    *queryDistributorLevels({params}, { select, call, put}) {
      const {result: data} = yield call(queryDistributorLevels, params);
      yield put({
        type: 'setLevels',
        levels: (data.levelItemList ? data.levelItemList : [])
      });
    },
    *editDistributor({params}, { select, call, put}) {
      const {result: data} = yield call(editDistributor, params);
      yield put({
        type: 'toggleModal',
        currentItem: {},
        field: 'editVisible',
        flag: false
      });
      _tempItem.displayName = params.distributorDisplayName;
      const list = yield select(({ distributorList }) => distributorList.list);
      const pagination = yield select(({ distributorList }) => distributorList.pagination);
      const hash = yield select(({ distributorList }) => distributorList.hash);
      yield put({
        type: 'setDistributorList',
        payload: {
          list: list,
          pagination: pagination,
          hash: hash
        }
      });
    }
  },

  reducers: {
    setDistributorList(state, {payload}) {
      return({
        ...state,
        list: payload.list,
        pagination: payload.pagination,
        hash: payload.hash
      })
    },
    toggleModal(state, {currentItem, field, flag}) {
      return({
        ...state,
        currentItem,
        [field]: flag
      })
    },
    changeFilter(state, {queryParam}) {
      return({
        ...state,
        queryParam
      })
    },
    setLevels(state, {levels}) {
      return({
        ...state,
        levels
      })
    },
    reset(state){
      let _temp = [{
        name: '全部',
        value: ''
      }];
      for(let i in registryTypes){
        _temp.push({
          name: registryTypes[i],
          value: i
        })
      }
      return{
        ...state,
        isExpand: false,
        queryParam: {},
        pagination: {
          current: 1,
          total: 0,
          showTotal: total => `共 ${total} 条`,
          showSizeChanger: true,
          showQuickJumper: true,
          defaultPageSize: 10,
          pageSize: 10
        },
        list: [],
        detailVisible: false,
        editVisible: false,
        currentItem: {},
        currentIndex: null,
        registryTypes: registryTypes,
        registryTypeList: _temp
      }
    }
  }
})

